package com.taru.wxhsh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taru.wxhsh.common.JsonResult;
import com.taru.wxhsh.constant.JResponseConstants;
import com.taru.wxhsh.pojo.User;
import com.taru.wxhsh.service.UserService;
import com.taru.wxhsh.util.UidGenerators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserController {
    Logger logger  = LoggerFactory.getLogger(UserController.class);
    @Autowired
    UserService  userService;

    /**
     * 查询用户列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/user/list")
    @ResponseBody
    public Object  list( @RequestParam(value = "pageNum" ,defaultValue = "1" ,required = false) Integer pageNum,
                         @RequestParam(value = "pageSize" ,defaultValue = "8" ,required = false) Integer pageSize,
                        @RequestParam(value = "telphone" ,defaultValue = "" ,required = false) String telphone,
                        @RequestParam(value = "shopName" ,defaultValue = "" ,required = false) String shopName  ){
        JsonResult<  PageInfo<User>>   result=null;
        try{
            PageHelper.startPage(pageNum,pageSize);
            List<User> users =userService.findUsersList(telphone,shopName);
            PageInfo<User>  pageInfo =new PageInfo<>(users,pageSize);
            result =new JsonResult<  PageInfo<User>>(JResponseConstants.STATUS_OK,"查询用户列表成功",pageInfo);
        }catch (Exception ex){
            logger.error("查询用户列表异常:{}",ex.getMessage());
            result =new JsonResult<  PageInfo<User>>(JResponseConstants.STATUS_ERROR,"查询用户列表异常");
        }
        return  result;


    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping("/user/add")
    public Object userAdd(User user){
        JsonResult<String> result = null;
        try {
            user.setUserId(UidGenerators.getUuid());
            userService.addUser(user);
            result = new JsonResult<>(JResponseConstants.STATUS_OK,"添加用户成功");
        }catch (Exception ex){
            logger.error("添加用户异常：{}",ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"添加用户异常");
        }
        return result;
    }

    /**
     * 删除用户
     * @param uid
     * @return
     */
    @ResponseBody
    @RequestMapping("/user/del")
    public Object userDel(String uid){
        JsonResult<String> result = null;
        try {
            userService.deleteByPrimaryKey(uid);
            result = new JsonResult<>(JResponseConstants.STATUS_OK,"删除用户成功");
        }catch (Exception ex){
            logger.error("删除用户异常：{}",ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"删除用户异常");
        }
        return result;
    }

    /**
     * 根据id查询用户用作修改
     * @param uid
     * @return
     */
    @ResponseBody
    @RequestMapping("/user/findById")
    public Object findById(String uid){
        JsonResult<User> result = null;
        try {
            User user = userService.selectByPrimaryKey(uid);
            result = new JsonResult<>(JResponseConstants.STATUS_OK,"查询用户成功",user);
        }catch (Exception ex){
            logger.error("查询用户异常：{}",ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"查询用户异常");
        }
        return result;
    }

    /**
     * 用户修改
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping("/user/update")
    public Object userUpdate(User user){
        JsonResult<String> result = null;
        try {
            userService.updateByPrimaryKeySelective(user);
            result = new JsonResult<>(JResponseConstants.STATUS_OK,"修改用户成功");
        }catch (Exception ex){
            logger.error("修改用户异常：{}",ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"修改用户异常");
        }
        return result;
    }
}
